

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<script type="text/javascript" src="helpman_topicinit.js"></script>

<title>Variables [Enterprise Architect User Guide]</title>

<meta name="keywords" content="Enterprise Architect, Sparx Systems, UML, Code Template Syntax,Code Template Syntax Variables,Code Template Syntax Variable References,Code Template Syntax Variable Definitions,Variable,Variable References, Code Template Syntax,Variable References, Examples,Variable Definitions, Code Template Syntax,Variable Definitions, Examples">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta http-equiv="Content-Style-Type" content="text/css" />

<link type="text/css" href="default.css" rel="stylesheet" />
<style type="text/css">
  A, A:Visited
  {
    color: #00F;
  }

  A:Active, A:Hover
  {
    color: #F00;
  }

  OL
  {
    margin: 5px 0 5px 48px;
  }

  UL
  {
    margin-bottom: 3px;
    margin-top: 0;
  }

  #nsr
  {
    background-color: #D4DFFF;
  }

  #nsr A
  {
    text-decoration: none;
  }

  #mainbody
  {
    padding: 8px;
  }

  .p_BodyText
  {
    padding: 5px 0;
  }
</style>



<style type="text/css" media="screen">
  BODY
  {
    background-color: #FFFFFF;
  }

  #nsr
  {
    padding: 6px 6px 0 6px;
    border-bottom: none;
    vertical-align: top;
    z-index: 2;
    visibility: visible;
    left: 0;
    top: 0;
    position: absolute;
  }

  #mainbody
  {
    left: 0;
    top: 0;
    margin: 0;
    position: absolute;
    padding: 10px;
    overflow: auto;
    height: 100%;
    z-index: 1;
    background-repeat: no-repeat;
    background-position: bottom right;
    background-attachment: fixed;
  }
</style>

<style type="text/css" media="print">
  #nsr
  {
    visibility: none;
  }

  #mainbody
  {
    overflow: visible;
  }
</style>
<script type="text/javascript" language="JavaScript" src="nonscroll.js"></script>

</head>
<body scroll="no">

<div id="page">



        <div id="content">
          <div id="content-inner">
          



  
          <div id="nsr">
            <table border="0" cellspacing="0" cellpadding="4" width="100%">
              <tr valign="top">
                <td align="left">
  


                  <p class="p_Heading1"><span class="f_Heading1">Variables</span></p>



  
                </td>
                <td align="right">
                  <a href="introduction.htm" onmouseover="document.images.main.src='button_main_h.gif'" onmouseout="document.images.main.src='button_main.gif'">
                    <img name="main" src="button_main.gif" border=0 alt="Return to Introduction">
                  </a>&nbsp;
    
                  <a href="easl_properties.htm" onmouseover="document.images.prev.src='button_prev_h.gif'" onmouseout="document.images.prev.src='button_prev.gif'">
                    <img name=prev src="button_prev.gif" border=0 alt="Previous page" />
                  </a>&nbsp;
    
    
    
                  <a href="thecodetemplateeditor_2.htm" onmouseover="document.images.next.src='button_next_h.gif'" onmouseout="document.images.next.src='button_next.gif'">
                    <img name=next src="button_next.gif" border=0 alt="Next page" />
                  </a>
    
    
                </td>
              </tr>
            </table>
          </div>
          <div id="mainbody">
  


                <p class="p_BodyText"><span class="f_BodyText">Template variables provide a convenient way of storing and retrieving data within a template. This section explains how variables are <a href="variables.htm#vdef">defined</a> and <a href="variables.htm#varref">referenced</a>.</span></p>
<p class="p_SubHeadingL1"><a name="vdef"></a><span class="f_SubHeadingL1">Variable Definitions</span></p>
<p class="p_BodyText"><span class="f_BodyText">Variable definitions take the basic form:</span></p>
<p class="p_Code"><span class="f_Code">$&lt;name&gt; = &lt;value&gt;</span></p>
<p class="p_BodyText"><span class="f_BodyText">where </span><span class="f_BodyText" style="font-style: italic;">&lt;name&gt;</span><span class="f_BodyText"> can be any alpha-numeric sequence and </span><span class="f_BodyText" style="font-style: italic;">&lt;value&gt;</span><span class="f_BodyText"> is derived from a macro or another variable.</span></p>
<p class="p_BodyText"><span class="f_BodyText">A simple example definition would be:</span></p>
<p class="p_Code"><span class="f_Code">$foo = %className%</span></p>
<p class="p_BodyText"><span class="f_BodyText">Variables can be defined, using values from:</span></p>
<ul style="text-indent: 0px; margin-left: 30px; list-style-position: outside;">
<li><span class="f_Bulletlist1">Substitution, function or list macros</span></li>
<li><span class="f_Bulletlist1">String literals, enclosed within double quotation marks</span></li>
</ul>
<ul style="text-indent: 0px; margin-left: 30px; list-style-position: outside;">
<li><span class="f_Bulletlist1">Variable references.</span></li>
</ul>
<p class="p_SubHeadingL2"><span class="f_SubHeadingL2">Definition Rules</span></p>
<p class="p_BodyText"><span class="f_BodyText">The following rules apply to variable definitions:</span></p>
<ul style="text-indent: 0px; margin-left: 30px; list-style-position: outside;">
<li><span class="f_Bulletlist1">Variables have global scope within the template in which they are defined and are not accessible to other templates</span></li>
<li><span class="f_Bulletlist1">Each variable must be defined at the start of a line, without any intervening whitespace</span></li>
<li><span class="f_Bulletlist1">Variables are denoted by prefixing the name with $, as in $foo</span></li>
<li><span class="f_Bulletlist1">Variables do not have to be declared, prior to being defined</span></li>
<li><span class="f_Bulletlist1">Variables must be defined using either the assignment operator (=), or the addition-assignment operator (+=)</span></li>
<li><span class="f_Bulletlist1">Multiple terms can be combined in a single definition using the addition operator (+).</span></li>
</ul>
<p class="p_SubHeadingL2"><span class="f_SubHeadingL2">Examples</span></p>
<p class="p_BodyText"><span class="f_BodyText">Using a substitution macro:</span></p>
<p class="p_Code"><span class="f_Code">$foo = %opTag:&quot;bar&quot;%</span></p>
<p class="p_BodyText"><span class="f_BodyText">Using a literal string:</span></p>
<p class="p_Code"><span class="f_Code">$foo = &quot;bar&quot;</span></p>
<p class="p_BodyText"><span class="f_BodyText">Using another variable:</span></p>
<p class="p_Code"><span class="f_Code">$foo = $bar</span></p>
<p class="p_BodyText"><span class="f_BodyText">Using a list macro:</span></p>
<p class="p_Code"><span class="f_Code">$ops = %list=&quot;Operation&quot; @separator=&quot;\n\n&quot; @indent=&quot;\t&quot;%</span></p>
<p class="p_BodyText"><span class="f_BodyText">Using the addition-assignment operator (</span><span class="f_BodyText" style="font-style: italic;">+=</span><span class="f_BodyText">):</span></p>
<p class="p_Code"><span class="f_Code">$body += %list=&quot;Operation&quot; @separator=&quot;\n\n&quot; @indent=&quot;\t&quot;%</span></p>
<p class="p_BodyText"><span class="f_BodyText">The above definition is equivalent to the following:</span></p>
<p class="p_Code"><span class="f_Code">$body = $body + %list=&quot;Operation&quot; @separator=&quot;\n\n&quot; @indent=&quot;\t&quot;%</span></p>
<p class="p_BodyText"><span class="f_BodyText">Using multiple terms:</span></p>
<p class="p_Code"><span class="f_Code">$templateArgs = %list=&quot;ClassParameter&quot; @separator=&quot;, &quot;%</span></p>
<p class="p_Code"><span class="f_Code">$template =&quot;template&lt;&quot; + $templateArgs + &quot;&gt;&quot;</span></p>
<p class="p_SubHeadingL1"><a name="varref"></a><span class="f_SubHeadingL1">Variable References</span></p>
<p class="p_BodyText"><span class="f_BodyText">Variable values can be retrieved by using a reference of the form:</span></p>
<p class="p_Code"><span class="f_Code">$&lt;name&gt;</span></p>
<p class="p_BodyText"><span class="f_BodyText">where &lt;name&gt; can be a previously defined variable.</span></p>
<p class="p_BodyText"><span class="f_BodyText">Variable references can be used in one of the following ways:</span></p>
<ul style="text-indent: 0px; margin-left: 30px; list-style-position: outside;">
<li><span class="f_Bulletlist1">As part of a macro, such as the argument to a function macro</span></li>
<li><span class="f_Bulletlist1">As a term in a variable definition</span></li>
<li><span class="f_Bulletlist1">As a direct substitution of the variable value into the output.</span></li>
</ul>
<div style="text-align: left; text-indent: 0px; padding: 0px 0px 0px 0px; margin: 5px 0px 5px 13px;"><table cellspacing="0" cellpadding="2" border="0" style="border: none; border-spacing:0px;">
<tr style="text-align:left;vertical-align:bottom;">
<td valign="bottom" width="952" bgcolor="#efeff7" style="width:952px; background-color:#efeff7;"><p class="p_TiporNote" style="border-top: none; border-right: none; border-left: none;"><span class="f_TiporNote">Note:</span></p>
</td>
</tr>
<tr style="text-align:left;">
<td width="952" bgcolor="#f7f7ff" style="width:952px; background-color:#f7f7ff;"><p class="p_TiporNoteText"><span class="f_TiporNoteText">It is legal to reference a variable before it is defined. In this case, the variable is assumed to contain an empty string value: &quot;&quot;</span></p>
</td>
</tr>
</table>
</div>
<p class="p_SubHeadingL2"><span class="f_SubHeadingL2">Example 1</span></p>
<p class="p_BodyText"><span class="f_BodyText">Using variables as part of a macro. The following is an excerpt from the default C++ ClassNotes template.</span></p>
<div style="text-align: left; text-indent: 0px; padding: 0px 0px 0px 0px; margin: 5px 0px 5px 13px;"><table cellspacing="0" cellpadding="0" border="0" style="border: none; border-spacing:0px;">
<tr style="text-align:left;vertical-align:top;">
<td valign="top" width="301" style="width:301px;"><p class="p_Code"><span class="f_Code">$wrapLen = %genOptWrapComment%</span></p>
<p class="p_Code"><span class="f_Code">$style = %genOptCPPCommentStyle%</span></p>
<p class="p_Code"><span class="f_Code">&nbsp;</span></p>
<p class="p_Code"><span class="f_Code">%if $style == &quot;XML.NET&quot;%</span></p>
<p class="p_Code"><span class="f_Code">%XML_COMMENT($wrapLen)%</span></p>
<p class="p_Code"><span class="f_Code">%else%</span></p>
<p class="p_Code"><span class="f_Code">%CSTYLE_COMMENT($wrapLen)%</span></p>
<p class="p_Code"><span class="f_Code">%endIf%</span></p>
</td>
<td valign="top" width="350" style="width:350px;"><p class="p_Tabletext"><span class="f_Tabletext">Define variables to store the style and wrap length options.</span><br>
<span class="f_Tabletext">&nbsp;</span></p>
<p class="p_Tabletext"><span class="f_Tabletext">Reference to </span><span class="f_Tabletext" style="font-style: italic;">$style</span><span class="f_Tabletext"> as part of a condition.</span><br>
<span class="f_Tabletext">&nbsp;</span></p>
<p class="p_Tabletext"><span class="f_Tabletext">Reference to </span><span class="f_Tabletext" style="font-style: italic;">$wrapLen</span><span class="f_Tabletext"> as an argument to function macro.</span></p>
</td>
</tr>
</table>
</div>
<p class="p_SubHeadingL2"><span class="f_SubHeadingL2">Example 2</span></p>
<p class="p_BodyText"><span class="f_BodyText">Using variable references as part of a variable definitions:</span></p>
<div style="text-align: left; text-indent: 0px; padding: 0px 0px 0px 0px; margin: 5px 0px 5px 13px;"><table cellspacing="0" cellpadding="0" border="0" style="border: none; border-spacing:0px;">
<tr style="text-align:left;vertical-align:top;">
<td valign="top" width="301" style="width:301px;"><p class="p_Code"><span class="f_Code">$foo = &quot;foo&quot;</span></p>
<p class="p_Code"><span class="f_Code">$bar = &quot;bar&quot;</span></p>
<p class="p_Code"><span class="f_Code">&nbsp;</span></p>
<p class="p_Code"><span class="f_Code">$foobar = $foo + $bar</span></p>
</td>
<td valign="top" width="348" style="width:348px;"><p class="p_Tabletext"><span class="f_Tabletext">Define our variables.</span></p>
<p class="p_Tabletext"><span class="f_Tabletext" style="font-style: italic;">$foobar</span><span class="f_Tabletext"> now contains the value </span><span class="f_Tabletext" style="font-style: italic;">foobar.</span></p>
</td>
</tr>
</table>
</div>
<p class="p_SubHeadingL2"><span class="f_SubHeadingL2">Example 3</span></p>
<p class="p_BodyText"><span class="f_BodyText">Substituting variable values into the output</span></p>
<div style="text-align: left; text-indent: 0px; padding: 0px 0px 0px 0px; margin: 5px 0px 5px 13px;"><table cellspacing="0" cellpadding="0" border="0" style="border: none; border-spacing:0px;">
<tr style="text-align:left;vertical-align:top;">
<td valign="top" width="306" style="width:306px;"><p class="p_Code"><span class="f_Code">$bases=%classInherits%</span></p>
<p class="p_Code"><span class="f_Code">&nbsp;</span></p>
<p class="p_Code"><span class="f_Code">Class %className%$bases</span></p>
</td>
<td valign="top" width="344" style="width:344px;"><p class="p_Tabletext"><span class="f_Tabletext">Store the result of the </span><span class="f_Tabletext" style="font-style: italic;">ClassInherits</span><span class="f_Tabletext"> template in </span><span class="f_Tabletext" style="font-style: italic;">$bases.</span></p>
<p class="p_Tabletext"><span class="f_Tabletext">Now output the value of </span><span class="f_Tabletext" style="font-style: italic;">$bases</span><span class="f_Tabletext"> after the Class name.</span></p>
</td>
</tr>
</table>
</div>




            </div>
          </div>
        </div>



</div>


</body>
</html>
